package com.cq.hd.app.controller;

import com.cq.hd.common.annotation.LoginUser;
import com.cq.hd.common.entity.AppLoginUser;
import com.cq.hd.common.entity.BusinessInfo;
import com.cq.hd.common.entity.UserInfo;
import com.cq.hd.common.enums.biz.ConfigSettingKeyEnum;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.Throw;
import com.cq.hd.member.api.TbAgentUnBindApplyApi;
import com.cq.hd.member.api.TbBusinessAgentApi;
import com.cq.hd.member.api.TbConfigSettingApi;
import com.cq.hd.member.api.dto.AgentUnBindApplyDto;
import com.cq.hd.member.api.dto.ConfigSettingQueryDto;
import com.cq.hd.member.api.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/agent")
@Api(tags = "经纪人相关接口")
public class AgentController extends BaseController {

    @Autowired
    private TbBusinessAgentApi businessAgentApi;

    @Autowired
    private TbAgentUnBindApplyApi agentUnBindApplyApi;

    @Autowired
    private TbConfigSettingApi configSettingApi;

    @ApiOperation(value = "我的上级经纪人")
    @GetMapping("/myParentAgent")
    public Result<MyAgentVo> myParentAgent(@ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        return businessAgentApi.myParentAgent(appLoginUser.getUserId());
    }

    @ApiOperation(value = "二级经纪人解绑申请")
    @PostMapping("/unBindApply")
    public Result<Boolean> unBindApply(@ApiIgnore @LoginUser AppLoginUser appLoginUser,
                                @RequestBody AgentUnBindApplyDto agentUnbindApplyDto) {
        agentUnbindApplyDto.setUserId(appLoginUser.getUserId());
        return agentUnBindApplyApi.apply(agentUnbindApplyDto);
    }

    @ApiOperation(value = "获取通用二级佣金比例")
    @GetMapping("/getDefaultAgent2CommissionRate")
    public Result<String> getDefaultAgent2CommissionRate(@ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        if (appLoginUser == null) {
            Throw.isBusinessException("请先登录");
        }
        return configSettingApi.getUserConfigValue(appLoginUser.getUserId(), ConfigSettingKeyEnum.DEFAULT_LEVEL2_COMMISSION_RATE.name());
    }

    @ApiOperation(value = "分销设置列表")
    @GetMapping("/setting/list")
    public Result<List<ConfigSettingVo>> settingList(@ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        if (appLoginUser == null) {
            Throw.isBusinessException("请先登录");
        }
        UserInfo userInfo = appLoginUser.getUserInfo();
        BusinessInfo businessInfo = userInfo.getBusinessInfo();
        ConfigSettingQueryDto configSettingQueryDto = new ConfigSettingQueryDto();
        configSettingQueryDto.setUserId(appLoginUser.getUserId());
        List<String> configKeys;
        if (businessInfo != null) {
            configKeys = Arrays.asList(ConfigSettingKeyEnum.BUSINESS_AGENT_APPLY_ENABLE.name(), ConfigSettingKeyEnum.AGENT_AUTO_APPROVAL.name());
        } else {
            configKeys = Arrays.asList(ConfigSettingKeyEnum.AGENT_AUTO_APPROVAL.name(), ConfigSettingKeyEnum.DEFAULT_LEVEL2_COMMISSION_RATE.name());
        }
        configSettingQueryDto.setConfigKeys(configKeys);
        return configSettingApi.list(configSettingQueryDto);
    }

}
